package com.zlj.request;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 获取客户端的IP地址、获取请求方式、获取User-Agent,得到客户端的信息(操作系统浏览器)
 */
@WebServlet("/RequestAServlet")
public class RequestAServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String IP = request.getRemoteAddr();   //获取客户端的IP地址
		System.out.println("客户端IP为：" + IP);  //客户端IP为：0:0:0:0:0:0:0:1
		
		String method = request.getMethod();    //获取请求方式
		System.out.println("请求方式为：" + method);   //请求方式为：GET
		
		String userAgent = request.getHeader("User-Agent");  //获取名为User-Agent的请求头
		//User-Agent为：Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:53.0) Gecko/20100101 Firefox/53.0
		System.out.println("User-Agent为：" + userAgent);
		
		if(userAgent.toLowerCase().contains("chrome")){
			System.out.println("您好，" + IP + ",用的浏览器是谷歌");
		}else if(userAgent.toLowerCase().contains("firefox")){
			System.out.println("您好，" + IP + ",用的浏览器是火狐");
		}else if(userAgent.toLowerCase().contains("msie")){
			System.out.println("您好，" + IP + ",用的浏览器是IE");
		}else{
			System.out.println("您好，" + IP + ",用的浏览器不知道是什么");
		}
	}
}
